package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DeleteTP
 */
@WebServlet(description = "刪除測試計畫案", urlPatterns = { "/DeleteTP" })
public class DeleteTP extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteTP() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tpid = request.getParameter("q_tpID"); // 測試案例ID
		String creator = request.getParameter("loginUser"); // 創建者
		
		String strSQL = "";
		String strRes = ""; // 最後執行的結果回傳
		
		
		Connection conn = null;
		Widgets wid = new Widgets();
		PreparedStatement ps = null;

		try {
			conn = wid.getConn(true);
			strSQL = "DELETE FROM tms_mritem_designer WHERE tpid='"+tpid+"'";
			ps = conn.prepareStatement(strSQL);
			ps.execute();
			
			strSQL = "DELETE FROM tms_mritem_programmer WHERE tpid='"+tpid+"'";
			ps = conn.prepareStatement(strSQL);
			ps.execute();
			
			strSQL = "DELETE FROM tms_mritem_propose WHERE tpid='"+tpid+"'";
			ps = conn.prepareStatement(strSQL);
			ps.execute();
			
			strSQL = "DELETE FROM tms_mritem_reference WHERE tpid='"+tpid+"'";
			ps = conn.prepareStatement(strSQL);
			ps.execute();
			
			strSQL = "DELETE FROM tms_mritem_requirelog WHERE tpid='"+tpid+"'";
			ps = conn.prepareStatement(strSQL);
			ps.execute();
			
			strSQL = "DELETE FROM tms_mritem_tester WHERE tpid='"+tpid+"'";
			ps = conn.prepareStatement(strSQL);
			ps.execute();
			
			strSQL = "DELETE FROM tms_mrtcmapping WHERE tpid='"+tpid+"'";
			ps = conn.prepareStatement(strSQL);
			ps.execute();
			
			strSQL = "DELETE FROM tms_mrtr WHERE tpid='"+tpid+"'";
			ps = conn.prepareStatement(strSQL);
			ps.execute();
			
			strSQL = "DELETE FROM tms_mrcomment WHERE tpid='"+tpid+"'";
			ps = conn.prepareStatement(strSQL);
			ps.execute();
			
			strSQL = "DELETE FROM tms_tpitem WHERE tpid='"+tpid+"'";
			ps = conn.prepareStatement(strSQL);
			ps.execute();
			
			strSQL = "DELETE FROM tms_mrfileupload WHERE tpid='"+tpid+"'";
			ps = conn.prepareStatement(strSQL);
			ps.execute();
			
			strSQL = "DELETE FROM tms_tpowner WHERE tpid='"+tpid+"'";
			ps = conn.prepareStatement(strSQL);
			ps.execute();
			
			strSQL = "DELETE FROM tms_tp WHERE tpid='"+tpid+"'";
			ps = conn.prepareStatement(strSQL);
			ps.execute();
			
			wid.keeplog(Integer.parseInt(tpid), "", 0, 8, creator, conn, ps);
			strRes = "刪除測試計畫案成功!!";

		} catch (SQLException e) {
			strRes = wid.keepErrorMes(conn, ps, e, "", true);
		} finally {
			try {
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				strRes = e.toString(); 
				System.out.println(strRes);
			}
		}

		response.setContentType("application/text");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(strRes);

	}

}
